package com.csist.dao.UserDaoImpl;

import com.csist.dao.UserDao;
import com.csist.entity.Users;
import com.csist.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public Users login(String passname, String userpass, String type) {
        String sql = "select * from users where user_name= ? and userpass=? and user_type=?";
        Object[] param = {passname,userpass,type};
        ResultSet rs = null;
        rs = JDBCUtil.executeQuery(sql,param);
        try {
            if(rs.next()) {
                Users users = new Users();
               users.setUsername(rs.getString("user_name"));
               users.setUserpass(rs.getString("userpass"));
               users.setTypenum(rs.getInt("user_type"));
                return users;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCountClass(String classid) {
        String sql="select count(*) from class where teacher_id=? and class_status=1";
        Connection conn=JDBCUtil.getConnection();
        ResultSet rs=null;
        PreparedStatement ps=null;
        int num = 0;
        try {
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()) {
                num=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            JDBCUtil.close(conn, ps, rs);
        }
        return num;
    }

    @Override
    public int getCountStudent(String teacherid) {
        String sql="select count(*) from student where teacher_id=? and studnet_status=1";
        Connection conn=JDBCUtil.getConnection();
        ResultSet rs=null;
        PreparedStatement ps=null;
        int num = 0;
        try {
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()) {
                num=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            JDBCUtil.close(conn, ps, rs);
        }
        return num;
    }
}
